import { Component, OnInit } from '@angular/core';
import { AGENT_BALANCE_TYPE, CAKE_ORDER_VOUCHER_STATUS_OPTION, USER_ROLE_OPTION } from '../../../constant/dictionary';
import { FinancesService } from '@core/routes-services/finances.service';
import { ActivatedRoute } from '@angular/router';
import { MyModalHelper } from '@core/my-modal.helper';
import { NzMessageService } from 'ng-zorro-antd';
import { CakeOrderViewComponent } from '../../mid-autumn/cake-order/order-view/cake-order-view.component';
import { ActivityProductService } from '@core/routes-services/activity-product.service';
import { MoonCakeService } from '@core/routes-services/moon-cake.service';

@Component({
  selector: 'app-recharge-list',
  templateUrl: './recharge-list.component.html',
})
export class RechargeListComponent implements OnInit {

  isLoading = false;
  keyword: any;
  financeType: any;
  type: any = 'recharge';// shopping-购物，recharge-充值 chargeback-扣款
  page: any = { number: 1, size: 10, totalElements: 0, content: [] };
  userRoleOption: any [] = USER_ROLE_OPTION;
  agentBalanceType: any [] = AGENT_BALANCE_TYPE;
  allProducts: any [] = [];

  constructor(private message: NzMessageService,
              private route: ActivatedRoute,
              private financesService: FinancesService,
              private activityProductService: ActivityProductService,
              private moonCakeService:MoonCakeService,
              private modalHelper: MyModalHelper) {
  }

  ngOnInit() {
    this.initProduct();
    this.initPage();
  }

  initProduct() {
    this.activityProductService.getAll().then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.allProducts = res.data.activityProductsList;
      } else {
        this.message.warning(res.message);
      }
    }, error => {
      this.message.error(error.message ? error.message : error.error);
    });
  }

  initPage() {
    this.isLoading = true;
    this.financesService.inPagination(this.page, this.keyword, this.type,this.financeType).then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.page = res.data;
        ++this.page.number;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }

  /**
   * 搜索重置分页数据
   */
  search() {
    this.page = { number: 0, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  changePageNo(pageNo) {
    this.page = { number: pageNo, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  seeEntity = (orderNo) => {
    this.moonCakeService.getByOrderNo(orderNo).then((res) => {
      if (res.status.toString().startsWith('20')) {
        if(!res.data){
          this.message.warning("查无此订单");
          return;
        }
        this.modalHelper.open(CakeOrderViewComponent, {
          entity: res.data,
          allProducts: this.allProducts,
          statusOption: CAKE_ORDER_VOUCHER_STATUS_OPTION,
        }, 'lg', {
          nzTitle: '订单信息',
        }).subscribe(result => {
          console.log('弹窗回调ok');
        }, (error) => {
          console.error('弹窗回调出错：', error);
        });
      } else {
        this.message.warning(res.message);
      }
    }, error => {
      this.message.error(error.message ? error.message : error.error);
    });

  };


}
